﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.ComponentModel;



namespace LoadingClass
{
    public class BackgroundLoading
    {
        public delegate void RunFunction();

        public BackgroundWorker Bw;
        public RunFunction thisFunction;
        LoadingForm newLoadingForm;
        string m_text;

        public BackgroundLoading(RunFunction i_newFunction, string i_text)
        {
            thisFunction = i_newFunction;
            Bw = new BackgroundWorker();
            Bw.DoWork += new DoWorkEventHandler(Bw_DoWork);
            Bw.RunWorkerCompleted +=  new RunWorkerCompletedEventHandler(Bw_RunWorkerCompleted);
            m_text = i_text;
        }

        public void Start()
        {
            Bw.RunWorkerAsync();
            newLoadingForm = new LoadingForm(m_text);
            newLoadingForm.ShowDialog();
        }

        void Bw_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
        {
            newLoadingForm.Dispose();
        }


        void Bw_DoWork(object sender, DoWorkEventArgs e)
        {
            if (thisFunction != null)
            {
                thisFunction();
            }
        }

    }
}
